package cn.wolfcode.trip.app.controller;

import cn.wolfcode.trip.base.domain.Accumulate;
import cn.wolfcode.trip.base.domain.Bankcard;
import cn.wolfcode.trip.base.domain.Record;
import cn.wolfcode.trip.base.domain.User;
import cn.wolfcode.trip.base.service.IAccumulateService;
import cn.wolfcode.trip.base.service.IBankcardService;
import cn.wolfcode.trip.base.service.IRecordService;
import cn.wolfcode.trip.base.service.IUserService;
import cn.wolfcode.trip.base.util.JSONResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("bankcards")
public class BankcardController {
    @Autowired
    private IBankcardService bankcardService;
    @Autowired
    private IUserService userService;
    @Autowired
    private IRecordService recordService;

    @Autowired
    private IAccumulateService accumulateService;

    /**
     * 查询登陆用户的所有银行卡
     *
     * @return
     */
    @GetMapping("{id}")
    public List<Bankcard> getAll(@PathVariable Long id) {
        List<Bankcard> bankcards = bankcardService.selectAllByUserId(id);
        return bankcards;
    }

    /**
     * 提现到银行卡
     *
     * @return
     */
    @PutMapping
    public JSONResult recharge(User user) {
        JSONResult json = new JSONResult();
        Record record = new Record();
        Accumulate a = new Accumulate();
        a.setUser(user);
        a.setDeposit(user.getBalance());
        //判断添加累计金额添加金额
        Accumulate accumulate = accumulateService.selectOrUser(user.getId());//查询有这个用户在累计添加数据
        if (accumulate == null) {
            accumulateService.saveUser(a);
        }else{
            accumulateService.updateBalance(user.getId(),user.getBalance());
        }

        //查询提现到那张银行卡
        Bankcard b = bankcardService.selectBankcardName(user.getId());
        record.setCashBackTime(new Date());//什么时候提现的
        record.setState(Record.STATE_DEPOSIT);//提现状态
        record.setMoney(user.getBalance());//提现金额
        record.setUser(user);//那个用户提现的
        record.setBankcard(b);//哪张银行卡
        recordService.insert(record);

        //判断提现的金额不能超过余额
        try {
            userService.updateMoney(user);
            bankcardService.updateMoney(user.getId(), user.getBalance());
            //查询更新后的数据
            User u = userService.selectByUserId(user.getId());
            json.setResult(u);
        } catch (Exception e) {
            json.mark(e.getMessage());
            e.printStackTrace();
        }
        return json;
    }

    /**
     * 添加银行卡
     *
     * @param bankcard
     * @return
     */
    @PostMapping("save")
    public JSONResult saveBankcard(Bankcard bankcard) {
        JSONResult json = new JSONResult();
        try {
            bankcardService.insetBankcard(bankcard);
        } catch (Exception e) {
            System.out.println(e.getMessage());
            json.mark(e.getMessage());
            e.printStackTrace();
        }
        return json;
    }

    /**
     * 回显银行卡数据
     *
     * @return
     */
    @GetMapping("/quantity")
    public Integer quantity() {
        Integer quantity = bankcardService.selectCount();
        return quantity;
    }
}
